package com.jiyun.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.mapper.NUserauthformMapper;
import com.jiyun.pojo.NUserauthform;
import com.jiyun.service.NUserauthformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.*;
import java.util.List;
import java.util.Map;

import static cn.hutool.core.img.ImgUtil.read;


@Service
public class NUserauthformServiceImpl extends ServiceImpl<NUserauthformMapper, NUserauthform> implements NUserauthformService {

    @Autowired
    private NUserauthformMapper nUserauthformMapper;

    @Override
    public void yanzheng(NUserauthform userauthform) {
        //需要从前端获取账户id，然后关联起来
        NUserauthform nUserauthform = new NUserauthform();
        nUserauthform.setUsername(userauthform.getUsername());
        nUserauthform.setUsercard(userauthform.getUsercard());
        //记的添加用户id
        nUserauthformMapper.insert(nUserauthform);
        //需要查实名认证的身份证号

        String host = "https://idcert.market.alicloudapi.com"; // 【1】请求地址 支持http 和 https 及 WEBSOCKET
        String path = "/idcard";// 【2】后缀
        String appcode = "d47c511d043b453bad12b0579f9908fc"; // 【3】开通服务后 买家中心-查看AppCode
        String idCard = userauthform.getUsercard();// 【4】请求参数，详见文档描述
        String name = userauthform.getUsername();// 【4】请求参数，详见文档描述
        try {
            String urlSend = host + path + "?idCard=" + idCard + "&name="+ URLEncoder.encode(name, "UTF-8");// 【5】拼接请求链接
            URL url = new URL(urlSend);
            HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
            httpURLCon.setRequestProperty("Authorization", "APPCODE " + appcode);//格式Authorization:APPCODE (中间是英文空格)
            int httpCode = httpURLCon.getResponseCode();
            if (httpCode == 200) {
                String json = String.valueOf(read(httpURLCon.getInputStream()));
                System.out.println("正常请求计费(其他均不计费)");
                System.out.println("获取返回的json：");
                System.out.print(json);
            } else {
                Map<String, List<String>> map = httpURLCon.getHeaderFields();
                String error = map.get("X-Ca-Error-Message").get(0);
                if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
                    System.out.println("AppCode错误 ");
                } else if (httpCode == 400 && error.equals("Invalid Url")) {
                    System.out.println("请求的 Method、Path 或者环境错误");
                } else if (httpCode == 400 && error.equals("Invalid Param Location")) {
                    System.out.println("参数错误");
                } else if (httpCode == 403 && error.equals("Unauthorized")) {
                    System.out.println("服务未被授权（或URL和Path不正确）");
                } else if (httpCode == 403 && error.equals("Quota Exhausted")) {
                    System.out.println("套餐包次数用完 ");
                } else if (httpCode == 403 && error.equals("Api Market Subscription quota exhausted")) {
                    System.out.println("套餐包次数用完，请续购套餐");
                } else {
                    System.out.println("参数名错误 或 其他错误");
                    System.out.println(error);
                }
            }
        } catch (MalformedURLException e) {
            System.out.println("URL格式错误");
        } catch (UnknownHostException e) {
            System.out.println("URL地址错误");
        } catch (Exception e) {
            // 打开注释查看详细报错异常信息
            // e.printStackTrace();
        }
    }
}
